package com.xiaomi.front.controller;

import com.xiaomi.front.service.advertisement.AdvertisementService;
import com.xiaomi.front.service.commoditys.CommService;
import com.xiaomi.front.service.malltype.MalltypeService;
import com.xiaomi.front.service.shopp_cart.ShoppCartService;
import com.xiaomi.pojo.Advertisement;
import com.xiaomi.pojo.Commoditys;
import com.xiaomi.pojo.Malltype;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private MalltypeService malltypeService;

    @Autowired
    private CommService commService;

    @Autowired
    private AdvertisementService advertisementService;

    @Autowired
    private ShoppCartService shoppCartService;

    //跳转到小米商城首页
    @RequestMapping("/index")
    public String Index(HttpSession session) {

        //读取商品分类列表
        List<Malltype> malltypes = malltypeService.MalltypeAll();
        session.setAttribute("malltypes", malltypes);

        //轮播图
        List<Advertisement> advertisements = advertisementService.FindAdver();
        session.setAttribute("advertisements", advertisements);

        //3/4级广告显示
        //第一个3级广告
        Advertisement Firstadver = advertisementService.Find3(0, 3);
        session.setAttribute("Firstadver", Firstadver);
        //第二个3级广告
        Advertisement Secondadver = advertisementService.Find3(1, 3);
        session.setAttribute("Secondadver", Secondadver);
        //第三个3级广告
        Advertisement Thirdadver = advertisementService.Find3(2, 3);
        session.setAttribute("Thirdadver", Thirdadver);
        //第四个3级广告
        Advertisement Endadver = advertisementService.Find3(3, 3);
        session.setAttribute("Endadver", Endadver);
        //第一个4级广告
        Advertisement FirstGrade4 = advertisementService.Find3(0, 4);
        session.setAttribute("FirstGrade4", FirstGrade4);
        //第2/3个4级广告
        Advertisement SecondGrade4 = advertisementService.Find3(1, 4);
        session.setAttribute("SecondGrade4", SecondGrade4);
        Advertisement thirdGrade4 = advertisementService.Find3(2, 4);
        session.setAttribute("thirdGrade4", thirdGrade4);
        //第4/5个4级广告
        Advertisement fourthGrade4 = advertisementService.Find3(3, 4);
        session.setAttribute("fourthGrade4", fourthGrade4);
        Advertisement fifthGrade4 = advertisementService.Find3(4, 4);
        session.setAttribute("fifthGrade4", fifthGrade4);

        //模块显示
        //手机模块
        List<Commoditys> phoneList = commService.Find(1);
        session.setAttribute("phoneList", phoneList);
        //家电模块
        List<Commoditys> houseList = commService.Find(4);
        session.setAttribute("houseList", houseList);
        //耳机音箱模块
        List<Commoditys> AiList = commService.Find(6);
        session.setAttribute("AiList", AiList);
        return "front/index";
    }


    //轮播图侧边分类 点击按类型查看商品
    @RequestMapping("/FindClass/{id}")
    public String FindClass(@PathVariable("id") int id, Model model) {

        //按商品类型查相关信息
        List<Commoditys> commoditys = commService.FindClass(id);
        //不可查到
        if (1 == commoditys.size() && null == commoditys.get(0) || 0 == commoditys.size()) {
            return "redirect:/commerr.jsp";
        } else {
            //可以查到
            model.addAttribute("classList", commoditys);
            return "front/malltype";
        }
    }

}
